package com.qiu.demo.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qiu.demo.entity.IdentityFile;
import com.qiu.demo.enums.HttpCodeEnum;
import com.qiu.demo.service.IdentityFileService;
import com.qiu.demo.util.JsonResult;
import com.qiu.demo.util.TokenUtil;
import com.qiu.demo.util.UploadFileUtils;

import cn.hutool.core.lang.UUID;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
@RequestMapping("/test/identity")
@CrossOrigin
public class IdentityFileController {

    @Resource
    private UploadFileUtils uploadFileUtils;

    @Resource
    private IdentityFileService identityFileService;

    @Value("${file.path}")
    private String filePath;
    @Value("${file.path}")
    private String scriptPath;

    @Resource
    private TokenUtil tokenUtil;

    @GetMapping("list")
    public JsonResult list(Integer pagenum, Integer pagesize, String filetype,
            HttpServletRequest request) {
        // Map userData = tokenUtil.parseToken(request.getHeader("Authorization"));
        // String userId = userData.get("userId").toString();
        String userId = "6";
        pagenum = 1;
        pagesize = 5;
        filetype = "tp";

        Page<IdentityFile> rowPage = new Page<>(pagenum, pagesize);
        LambdaQueryWrapper<IdentityFile> qw = new LambdaQueryWrapper<>();
        qw.eq(IdentityFile::getUserId, userId);
        Page<IdentityFile> filePage = identityFileService.page(rowPage, qw);

        List<IdentityFile> files = filePage.getRecords();

        return JsonResult.success().total(filePage.getTotal()).data("files", files);
    }

    @PostMapping("upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {

        String userId = "6";
        String path = uploadFileUtils.upload(file);

        if (path.equals("error")) {
            return JsonResult.error();
        }

        // String fileName = UploadFileUtils.getFileNameByFilePath(path);
        String fileName = file.getOriginalFilename();
        String fileType = "other";
        if (uploadFileUtils.checkFileType(fileName) == 1) {
            fileType = "Image";
        } else if (uploadFileUtils.checkFileType(fileName) == -1) {
            fileType = "Video";
        }

        IdentityFile identityFile = new IdentityFile();
        identityFile.setOrigrecFilePath(path);
        identityFile.setFileType(fileType);
        identityFile.setFilename(fileName);
        identityFile.setUploadTime(LocalDateTime.now());
        identityFile.setUserId(Integer.parseInt(userId));

        identityFileService.save(identityFile);

        // 返回图片访问地址
        return JsonResult.success();
    }

    @GetMapping("identity")
    public JsonResult getMethodName(String fileId) throws Exception {

        return JsonResult.success();
    }

}
